package com.jinxin.platform.cddjnhjcxtsysset.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cddjnhjcxtsysset.bean.CddjnhjcxtsyssetHomeAttach;
import com.jinxin.platform.cddjnhjcxtsysset.common.SysOrgInfo;
import com.jinxin.platform.cddjnhjcxtsysset.common.SysUserVO;
import com.jinxin.platform.cddjnhjcxtsysset.mapper.CddjnhjcxtsyssetHomeAttachMapper;
import com.jinxin.platform.cddjnhjcxtsysset.rpc.CddjnhjcxtsyssetRpc;
import com.jinxin.platform.cddjnhjcxtsysset.service.ICddjnhjcxtsyssetHomeAttachService;
import com.jinxin.platform.cddjnhjcxtsysset.utils.IdWorker;
import com.jinxin.platform.cddjnhjcxtsysset.utils.UploadFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-07-20
 * @Author: Lingnan
 **/
@Service
public class CddjnhjcxtsyssetHomeAttachServiceImpl implements ICddjnhjcxtsyssetHomeAttachService {
    @Autowired
    private CddjnhjcxtsyssetHomeAttachMapper homeAttachMapper;
    @Autowired
    private CddjnhjcxtsyssetRpc cddjnhjcxtsyssetRpc;
    @Resource
    private IdWorker idWorker;
    @Value("${platform.upload}")
    private String uploadPath;

    @Override
    public CddjnhjcxtsyssetHomeAttach selectOne(HashMap param) {
        return homeAttachMapper.selectOne(param);
    }

    @Override
    public List<CddjnhjcxtsyssetHomeAttach> list(HashMap param) {
        return homeAttachMapper.list(param);
    }

    @Override
    public JsonResult saveOrUpdate(CddjnhjcxtsyssetHomeAttach record) {
        if (record == null){
            return JsonResult.error("参数错误，操作失败");
        }
        String id = record.getId();
        int count;
        String msg = "";
        if(StringUtils.isEmpty(id)){
            record.setId(String.valueOf(idWorker.nextId()));
            record.setCreateTime(LocalDateTime.now());
            count = homeAttachMapper.insert(record);
            msg = "新增成功";
        }else {
            count = homeAttachMapper.updateByKey(record);
            msg = "更新成功";
        }
        if(count > 0){
            return JsonResult.ok(msg,"");
        }else {
            return JsonResult.error("操作失败，请重试");
        }
    }


    @Override
    public JsonResult uploadFile(MultipartFile file, String name) {
        SysUserVO currentUser = cddjnhjcxtsyssetRpc.getCurrentUser();
        if(currentUser == null){
            return JsonResult.error("获取当前用户信息失败");
        }
        if(StringUtils.isEmpty(name)){
           return JsonResult.error("缺少附件名称");
        }
        String filePath = UploadFileUtil.uploadFile(file, this.uploadPath, "");
        if(!StringUtils.isEmpty(filePath)){
            String orgId = currentUser.getOrgId();
            HashMap param = new HashMap();
            param.put("name",name);
            param.put("orgId",orgId);
            CddjnhjcxtsyssetHomeAttach homeAttach = homeAttachMapper.selectOne(param);
            int count;
            if(homeAttach == null){
                homeAttach = new CddjnhjcxtsyssetHomeAttach();
                homeAttach.setId(String.valueOf(idWorker.nextId()));
                homeAttach.setCreateTime(LocalDateTime.now());
                homeAttach.setName(name);
                homeAttach.setPath(filePath);
                homeAttach.setOrgId(orgId);
                homeAttach.setFileName(file.getOriginalFilename());
                count = homeAttachMapper.insert(homeAttach);
            }else {
                // 删除老的文件
                String path = homeAttach.getPath();
                if(!StringUtils.isEmpty(path)){
                    UploadFileUtil.deleteFile(this.uploadPath,path);
                }
                homeAttach.setCreateTime(LocalDateTime.now());
                homeAttach.setPath(filePath);
                homeAttach.setFileName(file.getOriginalFilename());
                count = homeAttachMapper.updateByKey(homeAttach);
            }
            if(count > 0){
                return JsonResult.ok("上传成功",filePath);
            }
        }
        return JsonResult.error("上传失败，请重试或联系管理员");
    }
}
